package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.GoodsDamageLost;
import com.ridaizu.model.Model;
import com.ridaizu.model.search.SearchModel;

@ManagedBean(name = "goodsDamageLostDao")
@ApplicationScoped
public class GoodsDamageLostDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from d_goods_damage_lost \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public List<GoodsDamageLost> selectGoodsDamageLostList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT \n");
		sql.append("gdl.id as id, \n");
		sql.append("gdl.transit_id as transit_id, \n");
		sql.append("t.transit_date as transit_date, \n");
		sql.append("gdl.goods_id as goods_id, \n");
		sql.append("g.name as good_name, \n");
		sql.append("gdl.damage_lost_id as damage_lost_id, \n");
		sql.append("d.name as damage_lost_name, \n");
		sql.append("gdl.count as count, \n");
		sql.append("gdl.remark as remark, \n");
		sql.append("gdl.rgt_opt as rgt_opt, \n");
		sql.append("gdl.rgt_date as rgt_date, \n");
		sql.append("gdl.upd_opt as upd_opt, \n");
		sql.append("gdl.upd_date as upd_date \n");
		sql.append("FROM d_goods_damage_lost gdl \n");
		sql.append("LEFT JOIN d_transit as t on t.id = gdl.transit_id \n");
		sql.append("LEFT JOIN m_goods as g on g.id = gdl.goods_id \n");
		sql.append("LEFT JOIN m_dictionary as d on d.id = gdl.damage_lost_id AND d.type = 1 \n");
		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<GoodsDamageLost> modelList = new ArrayList<GoodsDamageLost> ();
		while (rs.next()) {
			modelList.add((GoodsDamageLost)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		GoodsDamageLost goodsDamageLost = new GoodsDamageLost();
		goodsDamageLost.setId(rs.getInt("id"));
		goodsDamageLost.setTransitId(rs.getInt("transit_id"));
		goodsDamageLost.setTransitDate(rs.getTimestamp("transit_date"));
		goodsDamageLost.setGoodsId(rs.getInt("goods_id"));
		goodsDamageLost.setGoodsName(rs.getString("good_name"));
		goodsDamageLost.setDamageLostId(rs.getInt("damage_lost_id"));
		goodsDamageLost.setDamageLostName(rs.getString("damage_lost_name"));
		goodsDamageLost.setCount(rs.getInt("count"));
		goodsDamageLost.setRemark(rs.getString("remark"));
		goodsDamageLost.setRgtOpt(rs.getString("rgt_opt"));
		goodsDamageLost.setRgtDate(rs.getTimestamp("rgt_date"));
		goodsDamageLost.setUpdOpt(rs.getString("upd_opt"));
		goodsDamageLost.setUpdDate(rs.getTimestamp("upd_date"));

		return goodsDamageLost;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO d_goods_damage_lost \n");
		sql.append("( \n");
		sql.append("transit_id, goods_id, damage_lost_id, count, remark, \n");
		sql.append("rgt_opt, rgt_date, upd_opt, upd_date \n");
		sql.append(") \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?, ?, ?, ?, ?, ?); \n");

		GoodsDamageLost goodsDamageLost = (GoodsDamageLost) model;

		Object[] params = new Object[] {
				goodsDamageLost.getTransitId(), goodsDamageLost.getGoodsId(), goodsDamageLost.getDamageLostId(), goodsDamageLost.getCount(), goodsDamageLost.getRemark(),
				goodsDamageLost.getRgtOpt(), goodsDamageLost.getRgtDate(), goodsDamageLost.getUpdOpt(), goodsDamageLost.getUpdDate()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE d_goods_damage_lost \n");
		sql.append("SET \n");
		sql.append("transit_id = ?, \n");
		sql.append("goods_id = ?, \n");
		sql.append("damage_lost_id = ?, \n");
		sql.append("count = ?, \n");
		sql.append("remark = ?, \n");
		sql.append("rgt_opt = ?, \n");
		sql.append("rgt_date = ?, \n");
		sql.append("upd_opt = ?, \n");
		sql.append("upd_date = ? \n");
		sql.append("WHERE id = ? \n");

		GoodsDamageLost goodsDamageLost = (GoodsDamageLost) model;

		Object[] params = new Object[] {
				goodsDamageLost.getTransitId(), goodsDamageLost.getGoodsId(), goodsDamageLost.getDamageLostId(), goodsDamageLost.getCount(), goodsDamageLost.getRemark(),
				goodsDamageLost.getRgtOpt(), goodsDamageLost.getRgtDate(), goodsDamageLost.getUpdOpt(), goodsDamageLost.getUpdDate(), goodsDamageLost.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM d_goods_damage_lost WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
